package com.offics.loan.interfaces.controller;

import com.offics.loan.application.dto.bankcard.AddBankCardRequest;
import com.offics.loan.application.dto.bankcard.BankCardResponse;
import com.offics.loan.application.dto.bankcard.UpdateBankCardRequest;
import com.offics.loan.application.service.BankCardApplicationService;
import com.offics.loan.infrastructure.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 银行卡控制器
 */
@Tag(name = "银行卡", description = "银行卡相关接口")
@RestController
@RequestMapping("/api/bank-cards")
@RequiredArgsConstructor
@SecurityRequirement(name = "JWT")
public class BankCardController {

    private final BankCardApplicationService bankCardApplicationService;

    /**
     * 添加银行卡
     *
     * @param request 添加银行卡请求
     * @return 银行卡响应
     */
    @Operation(summary = "添加银行卡", description = "添加银行卡")
    @PostMapping
    public Result<Void> add(@Valid @RequestBody AddBankCardRequest request) {
        boolean result = bankCardApplicationService.add(request);
        return Result.success();
    }

    /**
     * 更新银行卡
     *
     * @param request 更新银行卡请求
     * @return 是否成功
     */
    @Operation(summary = "更新银行卡", description = "更新银行卡")
    @PutMapping
    public Result<Void> update(@Valid @RequestBody UpdateBankCardRequest request) {
        boolean result = bankCardApplicationService.update(request);
        return Result.success();
    }

    /**
     * 删除银行卡
     *
     * @param id 银行卡ID
     * @return 是否成功
     */
    @Operation(summary = "删除银行卡", description = "删除银行卡")
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        boolean result = bankCardApplicationService.delete(id);
        return result ? Result.success() : Result.failure("删除银行卡失败");
    }

    /**
     * 设置默认银行卡
     *
     * @param id 银行卡ID
     * @param userId 用户ID
     * @return 是否成功
     */
    @Operation(summary = "设置默认银行卡", description = "设置默认银行卡")
    @PutMapping("/{id}/default")
    public Result<Void> setDefault(@PathVariable Long id, @RequestParam Long userId) {
        boolean result = bankCardApplicationService.setDefault(id, userId);
        return Result.success();
    }

    /**
     * 启用银行卡
     *
     * @param id 银行卡ID
     * @return 是否成功
     */
    @Operation(summary = "启用银行卡", description = "启用银行卡")
    @PutMapping("/{id}/enable")
    public Result<Void> enable(@PathVariable Long id) {
        boolean result = bankCardApplicationService.enable(id);
        return result ? Result.success() : Result.failure("启用银行卡失败");
    }

    /**
     * 禁用银行卡
     *
     * @param id 银行卡ID
     * @return 是否成功
     */
    @Operation(summary = "禁用银行卡", description = "禁用银行卡")
    @PutMapping("/{id}/disable")
    public Result<Void> disable(@PathVariable Long id) {
        boolean result = bankCardApplicationService.disable(id);
        return result ? Result.success() : Result.failure("禁用银行卡失败");
    }

    /**
     * 获取银行卡
     *
     * @param id 银行卡ID
     * @return 银行卡响应
     */
    @Operation(summary = "获取银行卡", description = "获取银行卡")
    @GetMapping("/{id}")
    public Result<BankCardResponse> get(@PathVariable Long id) {
        BankCardResponse response = bankCardApplicationService.get(id);
        return Result.success(response);
    }

    /**
     * 获取用户的银行卡列表
     *
     * @param userId 用户ID
     * @return 银行卡响应列表
     */
    @Operation(summary = "获取用户的银行卡列表", description = "获取用户的银行卡列表")
    @GetMapping("/user/{userId}")
    public Result<List<BankCardResponse>> listByUserId(@PathVariable Long userId) {
        List<BankCardResponse> responses = bankCardApplicationService.listByUserId(userId);
        return Result.success(responses);
    }

    /**
     * 获取用户的默认银行卡
     *
     * @param userId 用户ID
     * @return 银行卡响应
     */
    @Operation(summary = "获取用户的默认银行卡", description = "获取用户的默认银行卡")
    @GetMapping("/user/{userId}/default")
    public Result<BankCardResponse> getDefaultByUserId(@PathVariable Long userId) {
        BankCardResponse response = bankCardApplicationService.getDefaultByUserId(userId);
        return Result.success(response);
    }
}
